package enumandlambda;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author shkstart
 * Callable的匿名内部类
 * @create 2022-11-28-14:41
 */
public class ThreadDemo9 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<String> futureTask = new FutureTask<>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                //新程序执行的业务代码
                String[] arrss = new String[]{"java", "myslq", "http"};
                //随机返回一个字符串
                String result = arrss[new Random().nextInt(3)];
                System.out.println(Thread.currentThread().getName()+
                        "--字符串：" + result);
                return result;
            }
        });
        //创建新线程
        Thread thread = new Thread(futureTask);
        //启动线程
        thread.start();
        String result = futureTask.get();
        System.out.println(Thread.currentThread().getName()+
                "--新线程的返回值："+ result);
    }
}
